#include "mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{

    this->resize(800,600);
    textEdit = new QTextEdit(this);
    this->setCentralWidget(textEdit);//设置中央布局组件
    //1、构造菜单栏、工具栏、状态栏对象并添加到mainwindow；
    menubar = new QMenuBar(this);//指定父对象回收，且该类基于父对象实例化
    this->setMenuBar(menubar);//添加菜单栏到主窗口
    toolbar = new QToolBar(this);
    this->addToolBar(toolbar);//添加工具栏
    statusbar = new QStatusBar(this);
    this->setStatusBar(statusbar);//添加状态栏

    //2、菜单栏添加菜单
    fileMenu =   menubar->addMenu("文件(&F)");//热键F，atl +F;//加&可以添加热键
    editMenu = menubar->addMenu("编辑(&E)");
    //3、添加菜单项(QAction)
    openAction = new QAction(QIcon(":/imgs/QQ.ico"),"QQ(&Q)",this);
    openAction->setShortcut(QKeySequence("ctrl+Q"));//设置快捷键
    fileMenu->addAction(openAction);//将openAction添加到fieldmenu菜单

    fileMenu->addSeparator();//添加分隔符
    editAction = new QAction(QIcon(":/imgs/opend_folder.ico"),"edit(&E)",this);
    editAction->setShortcut(QKeySequence("ctrl+E"));//设置快捷键
    fileMenu->addAction(editAction);//将openAction添加到fieldmenu菜单
    //4、在工具栏添加工具(QAction)
    toolbar->addAction(openAction);
    toolbar->addAction(editAction);
    //5、状态栏
    label = new QLabel(this);
    label->setText("??????");
    statusBar()->addWidget(label);

    linecol = new QLabel(this);
    linecol->setText("lin:0 clo:0\t");
    statusbar->addPermanentWidget(linecol);



    //关联信号与槽函数
    connect(openAction,SIGNAL(triggered(bool)),this,SLOT(openAction_slot()));
    connect(textEdit,SIGNAL(cursorPositionChanged()),this,SLOT(linUpdate()));//当前光标的行列值
}

MainWindow::~MainWindow()
{

}
void MainWindow:: openAction_slot(){
    QString filename = QFileDialog::getOpenFileName(this,"打开文件，QQ",QDir::currentPath(),"C++ Source files(*.cpp *.c)");
    qDebug()<<filename;
}
void MainWindow:: linUpdate(){
    int column = textEdit->textCursor().columnNumber();//光标的列
    int line = textEdit->textCursor().blockNumber();//光标的行
    linecol->setText("ln:"+ QString::number(line)+ " "+"col:"+QString::number(column));
}
